package com.dfbz.多线程版在线聊天;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class bobo {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.2.237", 8888);
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Scanner sc = new Scanner(System.in);
                        OutputStream os = socket.getOutputStream();
                        System.out.println("请输入消息");
                        os.write(("serverFTP: " + sc.nextLine()).getBytes());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        InputStream is = socket.getInputStream();
                        byte[] data = new byte[1024];
                        if (is.available() != 0) {
                            int len = is.read(data);
                            System.out.println(new String(data, 0, len));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
